/* 
    Project     : 48c6c450f1a4a0cc53d9585dc0fee742
    Created on  : Mar 16, 2013, 11:29:15 PM
    Author      : Truong Khuong - khuongxuantruong@gmail.com
    Description :
        Purpose of the stylesheet follows.
*/
var User=(function(){
    var init=false;
    var datafields=[
        { name  :   'ause_id' ,type :   'int'},
        { name  :   'ause_name' },
        { name  :   'ause_username' },
        { name  :   'ause_email' },
        { name  :   'ause_authority' },
        { name  :   'ause_position',type    :   'int' },
        { name  :   'ause_status', type : 'bool' },
        { name  :   'ause_insert' },
        { name  :   'ause_update' },
    ],
    columns=[
        { text: 'Name'      , dataField: 'ause_name'  },
        { text: 'UserName'      , dataField: 'ause_username'  },
        { text: 'Mail'      , dataField: 'ause_email'  },
        { text: 'Authority' , dataField: 'ause_authority'     ,width:100,filterable:false },
        { text: 'Position'  , dataField: 'ause_position'    ,width:80,filterable:false, cellsalign:'right',columntype:'numberinput',filtertype:'number'},
        { text: 'Status'    , dataField: 'ause_status'      ,width:80,filterable:false ,columntype:'checkbox',threestatecheckbox:false,filtertype:'bool'},
        { text: 'Insert'    , dataField: 'ause_insert'      ,width:120,filterable:false ,cellsformat:'yyyy-MM-dd HH:mm:ss'},
        { 
            text: '#'        , dataField: 'ause_id'          ,width:80,filterable:false ,sortable: false,
            cellsrenderer : function (row, column, value) {
                var str="<span style='margin: 4px; float: left;'>";
                if(value && value>0){
                    try{
                        //value = $.parseJSON(value);
                        str+="\
                            <div onclick=\"User.EditItem('"+value+"');\" \
                            class='w-16 h-16 f-l m-r-4 c-s-p hover-50 edit-icon' title='Edit'></div>\
                        ";
                        str+="\
                            <div onclick=\"User.DeleteItem('"+value+"');\" \
                            class='w-16 h-16 f-l m-r-4 c-s-p hover-50 del-icon' title='Delete'></div>\
                        ";
                        str+="\
                            <div onclick=\"User.Permission('"+value+"');\" \
                            class='w-16 h-16 f-l m-r-4 c-s-p hover-50 user-icon' title='Permission'></div>\
                        ";
                    }catch(e){}
                }
                str+="</span>";
                return str;
            }
        },
    ],
    source,dataAdapter,
    grid_element='#jqxGrid_Backend';
    return {
        OnInit:function(){
            try{
                if(init) return;
                init=true;
                //grid_element=$('#jqxGrid_Backend_Categories');
                source ={
                    datatype: "json",type : "POST",
                    datafields: datafields,
                    id: 'ause_id',
                    url: base_url+'backend/user/binding',
                    filter      : function(){ $(grid_element).jqxGrid('updatebounddata', 'filter'); },
                    sort        : function(){ $(grid_element).jqxGrid('updatebounddata'); },
                    root        : 'rows',
                    beforeprocessing: function(data){ source.totalrecords = data.total_rows; }
                };
                dataAdapter = new $.jqx.dataAdapter(source, {
                    loadError: function(xhr, status, error){
                        ErrorMsg("<b>Status</b>:"+xhr.status+"<br/><b>ThrownError</b>:"+error+"<br/>");
                    }
                });
                $(grid_element).jqxGrid({
                    rendergridrows: function(obj){
                        return obj.data;    
                    },ready: function () {
                    },
                    width: '100%',//height:'100%',
                    source: dataAdapter,
                    theme: theme,sortable: true,
                    //filterable: true,
                    //autoshowfiltericon: true,
                    //showfilterrow: true,
                    showemptyrow: true,
                    virtualmode:false,
                    columns: columns
                });
                
            }catch(e){
                ErrorMsg(e.message);
            }
        },
        AddItem:function(){
            this.EditItem(0);
        },
        EditItem:function(Id){
            backendloading();
            $("#backend_edit_panel_popup").load(
            base_url+"backend/user/editor/" + Id,
                function() {
                    bckdialog({
                        title:'User',message:$('#backend_edit_panel_popup'),
                        buttons:{
                            'Save': function() {
                                menu.Save();
                            },
                            Cancel: function() {
                                $( this ).dialog( "close" );
                            }
                        }
                    }).open();
                    backendloaded();
                    $("#ause_frm").validationEngine();
                }
            );
        },
        Save:function(){
            if( $('#ausegory_form_edit_panel').validationEngine('validate') === false)return false;
            new backend({
                url         :   base_url+"backend/ausegory/save?type="+ausegory_type_index,
                data        :   {
                    params  :   $('#ausegory_form_edit_panel').serializeObject(),
                    type    :   ausegory_type_index
                },
                callback    :   function(rsdata){
                    if(rsdata.result<0){
                        bckdialog({type:'error'}).open(rsdata.message);
                    }else{
                        NoticeMsg(rsdata.message);
                        Categories.Cancel();
                        Categories.Refresh();
                    }
                    console.log(rsdata);
                }
            }).call();
            return false;
        },
        Cancel:function(){
            $('#ausegory_edit_panel_popup').dialog('close');
        },
        DeleteItem:function(id){
            //accessdenied();
            ConfirmMsg(function(){
                backend_delete('ausegory','ausegories','ause',id,function(){
                    $(grid_element).jqxGrid('updatebounddata');
                    $("#bckconfirmdialog").dialog('close');
                });
            },'These items will be permanently deleted and cannot be recovered. Are you sure ?','Delete');
            
        },
        RestoreItem:function(){
            accessdenied();
        },
        Permission:function(id){
            backendloading();
            $("#backend_edit_panel_popup").load(
            base_url+"backend/user/detail/" + id,
                function() {
                    backendloaded();
                    bckdialog({
                        title:'<span class="dotum">Change Permission</span>',
                        message:$('#backend_edit_panel_popup'),
                        buttons:{
                            Done: function() {
                                $( this ).dialog( "close" );
                            }
                        }
                    }).open();
                }
            );
        },
        changeprivilege:function(user_id,privilege_id,value,secretkey,signature){
            new backend({
                url         :   base_url+"backend/user/setprivilege",
                data        :   {
                    user_id		:	user_id,
                    privilege_id:	privilege_id,
                    permission	:	value,
                    secretkey	:	secretkey,
                    signature	:	signature
                },
                datatype    :   "json",
                callback    :   function(data){
                    try{
                        if(data.result<0){
                            bckdialog({
                                type:'error'
                            }).open(data.message);
                        }else{
                            NoticeMsg(data.message);
                        }
                    }catch(e){
                        bckdialog({
                            type:'error'
                        }).open("Sorry. Your request could not be completed.<br/> Please check your input data and try again.");
                    }
                    console.log(data);
                }
            }).call();
        },
        PrivateItem:function(){
            accessdenied();
        },
        Setting:function(){
            bckdialog({
                message:$('#window-sOpt'),
                icon:"<img class='p-a t-7 l-8' src='"+base_url+"/libraries/images/16/option.gif'/>",
                title:'Setting'
            }).open();
        },
        Refresh:function(){
            $(grid_element).jqxGrid('updatebounddata');
            //Msg('✔ Dép bông nhung mềm mịn - 68.000 | Bộ màu vẽ 68 món - 85.000 | Máy uốn tóc new - 135.000');
        },
        Filter:function(datafield,filtervalue){
            var filtertype = 'stringfilter';
            var filtergroup = new $.jqx.filter();
            var filter_or_operator = 1;
            var filtercondition = 'equal';
            var filter = filtergroup.createfilter(filtertype, filtervalue, filtercondition);
            filtergroup.addfilter(filter_or_operator, filter);
            
            //$(grid_element).jqxGrid('clearfilters');
            // add the filters.
            $(grid_element).jqxGrid('addfilter', datafield, filtergroup);
            // apply the filters.
            $(grid_element).jqxGrid('applyfilters');
        }
    };
}());
$(document).ready(function () {
    //Btree().Init();
});